<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img{
            display: block;
            height: 300px;
        }
    </style>
</head>
<body>
    <img src="" data-src="https://t7.baidu.com/it/u=1732966997,2981886582&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=1785207335,3397162108&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=2581522032,2615939966&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=245883932,1750720125&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=3423293041,3900166648&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=3241434606,2550606435&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=1417505637,1247476664&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=3659156856,3928250034&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=1416385889,2308474651&fm=193&f=GIF" alt="">
    <img src="" data-src="https://t7.baidu.com/it/u=2469680087,3014121106&fm=193&f=GIF" alt="">


    <script>
        
        let height = window.innerHeight
        //判断默认情况下应该展示那些图片
        function lazyLoad(){
            
            let imgs = document.querySelectorAll('img[data-src]');
            for(let i = 0;i<imgs.length;i++){
                let rect = imgs[i].getBoundingClientRect() //获取元素的几何属性
                if (rect.bottom >= 0 && rect.top <= height) {
                    let newImg = new Image()
                    newImg.src = imgs[i].getAttribute('data-src')    
                    newImg.onload = function () {//当图片加载完成后，将图片的src属性赋值为data-src属性的值
                        imgs[i].src = newImg.getAttribute('src')
                        
                    }
                    imgs[i].removeAttribute('data-src')
                }
            }

        }
        lazyLoad()
        window.addEventListener('scroll',lazyLoad)
    </script>
</body>
</html>